package com.quik.web.room.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.RoomType;
import com.quik.entity.SysAccount;
import com.quik.exception.ErrorCode;
import com.quik.web.product.service.ProductService;
import com.quik.web.room.service.RoomService;

/**
 * 功能说明：
 * 		   房间相关业务控制处理类
 * @author Jenny
 *
 */
@Controller
@RequestMapping("/roomType")
public class RoomTypeController {
	private static Logger log = (Logger)Logger.getLogger(RoomTypeController.class);
	
	@Autowired
	private RoomService roomService;
	
	@Autowired
	private ProductService productService;
	
	/**
	 * 进入房间类型界面
	 * 		（关联绑定的产品）
	 * @return
	 */
	@RequestMapping(value = "roomTypeList", method = RequestMethod.GET)
	public String roomTypeList(){
		return "/room/roomType/roomTypeList";
	}
	
	/**
	 * 房间类型
	 * @param page
	 * @param rows
	 * @param roomType
	 * @param productNo
	 * @param productName
	 * @return
	 */
	@RequestMapping(value = "queryRoomType", method = RequestMethod.POST)
	@ResponseBody
	public PaginationDto queryRoomType(Integer page, Integer rows, String roomTypeName, 
				String productNo, String productName){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.TREE_PAGE_SIZE : rows;  //每页显示的记录
		    
		    int totalCount = roomService.findRoomTypeCount(roomTypeName, productNo, productName);
		    List list = roomService.findRoomType(roomTypeName, productNo, productName, intPage, pageSize);
		    
		    dto.setTotal(totalCount);
		    dto.setRows(list);
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomTypeController queryRoomType error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 进入新增房间类型界面
	 * @return
	 */
	@RequestMapping(value = "initAddRoomType")
	public ModelAndView initAddRoomType(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/room/roomType/addRoomType");
		String result = (String)request.getAttribute("result");
		
		try{
			List categoryList = productService.findCategory();
			
			if(result != null && result.length() > 0)
				mav.addObject("result", result);
			
			mav.addObject("categoryList", categoryList);
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomTypeController initAddRoomType error: " + e.getMessage());
		}
		
		return mav;
	}
	
	/**
	 * 检查房间类型名称是否重名
	 * @param roomTypeName
	 * @return
	 */
	@RequestMapping(value = "checkRoomTypeName", method = RequestMethod.POST)
	public ResponseEntity<String> checkRoomTypeName(String roomTypeName){
		boolean result = false;
		
		try{
			result = roomService.checkRoomTypeName(roomTypeName.trim());
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomTypeController checkRoomTypeName error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(result), HttpStatus.OK);
		
	}
	
	/**
	 * 新增房间类型
	 * @param roomType
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addRoomType", method = RequestMethod.POST)
	public ModelAndView addRoomType(RoomType roomType, HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/room/roomType/addRoomType");
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		String result = ErrorCode.RESULT_ADD_ROOM_TYPE_FAILED;
		
		try{
			roomType.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			roomType.setCrt_user(account.getStaff_no());
			
			roomService.addRoomType(roomType);
			
			result = ErrorCode.RESULT_ADD_ROOM_TYPE_SUCCESS;
			
			
			mav.setViewName("forward:/roomType/initAddRoomType");
		}catch(Exception e){
			result = ErrorCode.SYSTEM_ERROR;
			e.printStackTrace();
			log.error("RoomTypeController addRoomType error: " + e.getMessage());
		}
		mav.addObject("result", result);
		
		return mav;
	}
}
